
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.Call;
using Swaf.Container;
using Swaf.Security;

namespace Swaf.Security
{
	/// <summary>
	/// This is a basic implementation of the SecurityPrincipalBase that allows
	/// the additionalinfo to be changed during run time.  This is not intended to
	/// be used to switch the user context out, but rather to allow it to be updated
	/// with a new bizobj of the same user.  It also does not attempt to cache any
	/// of the resourceset information like the standard SecurityPrincipal
	/// implementation does.</summary>
	/// <remarks>
	/// Because the origional SecurityPrincipal implementation contains a cache
	/// of resources from its resourcesets, it made it difficult to change
	/// the privilege level of the resources and which resources were and 
	/// were not available to the user.  This implementation always goes into
	/// its resourcesets collection to get the privilege level for a requested
	/// resource.</remarks>
	[Serializable]
    public class DynamicResourcesSecurityPrincipal : SecurityPrincipalBase	
    {
		/// <summary>
		/// Constructor for instancing a default security principal from code
		/// </summary>
		/// <param name="UserName">The name of the user associated with this principal</param>
		/// <param name="additionalInfo">INameAddressSupport instance containing application 
		/// specific information about the principal</param>
		/// <param name="rolesCall">ICall instance used to retrieve the roles for a principal.</param>
		public DynamicResourcesSecurityPrincipal(string UserName, INameAddressSupport additionalInfo, ICall rolesCall) 
			: base(UserName, additionalInfo, rolesCall)
		{
		}
		/// <summary>
		/// Constructor used by the configuration system or to deserialize a serialized principal from an Xml node.
		/// </summary>
		/// <param name="ele">XmlNode containing information about the principal</param>
		public DynamicResourcesSecurityPrincipal(XmlElement ele) 
			: base(ele)
		{
		}

		/// <summary>
		/// Allows the additional info to be updated after it was passed in during
		/// the constructor.</summary>
		/// <remarks>
		/// If an application provides a user interface for updating the additional info
		/// during runtime, this allows the application to adjust the information at
		/// runtime rather then making the user login/logoff to see the changes.</remarks>
		/// <param name="info">The new additional info to replace the existing one with.</param>
        public void updateAdditionalInfo(IBizObj info)
        {
            m_additionalInfo = info;
        }

	#region ISecurityPrincipal Members

		/// <summary>
		/// Implementation of abstract getPrivilige method from base class.  This implementation
		/// returns the privilege as defined in the requested ResourceSet.  If no privilege is
		/// found, Privileges.None is returned.
		/// </summary>
		/// <param name="resourceName">Fully qualified name of the requested resource (ResourceSet.Resource)</param>
		/// <returns></returns>
		public override Swaf.Security.Privileges getPrivilege(string resourceName)
		{
			Privileges thisPrivilege = (Privileges)m_resourceSets.get(resourceName);
			return thisPrivilege;
		}
		public override Swaf.Security.Privileges getPrivilege(string resourceName, bool useDefaultIfNeeded)
		{
			Privileges thisPrivilege = (Privileges)m_resourceSets.get(resourceName, useDefaultIfNeeded);
			return thisPrivilege;
		}


		#endregion
        public DynamicResourcesSecurityPrincipal(System.Runtime.Serialization.SerializationInfo info, StreamingContext context)
            : base(info, context)
		{}

	}
}
